package principal;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import entidade.Usuario;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

import cadastro.JDCartao;
import cadastro.JIFConta;
import cadastro.JDDivisao;
import cadastro.JDUsuario;
import lancamento.JIFAcerto;
import lancamento.JIFComovime;
import lancamento.JIFFatura;
import lancamento.JIFLancamento;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.text.ParseException;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.border.EmptyBorder;

import dao.Conexao;
import dao.CriaConexao;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

@SuppressWarnings({ "serial" })
public class JFPrincipal extends JFrame {

	private static JFPrincipal frame;
	private static JPanel contentPane;
	private static Usuario usuario;
	private JMenuBar menuBar;
	private JMenuItem mntmComovime;
	private JMenuItem mntmFatura_1;
	private JMenuItem mntmAcerto;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					// UIManager.setLookAndFeel(new TinyLookAndFeel());
					Conexao.getConexao();
					CriaConexao.getConexao();
					frame = new JFPrincipal();
					frame.setVisible(true);
					JDLogin frmLogin = new JDLogin(frame);
					frmLogin.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFPrincipal() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				try {
					Conexao.close();
					CriaConexao.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				JFPrincipal.class.getResource("/img/coingold.png")));
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setExtendedState(MAXIMIZED_BOTH);
		setTitle("Finnan");

		contentPane = new JPanel(new BorderLayout());
		contentPane.setBorder(new EmptyBorder(2, 2, 2, 2));
		setContentPane(contentPane);

		menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnCadastro = new JMenu("Cadastro");
		mnCadastro.setMnemonic('C');
		menuBar.add(mnCadastro);

		JMenuItem mntmCarto = new JMenuItem("Cart\u00E3o");
		mntmCarto.setIcon(new ImageIcon(JFPrincipal.class
				.getResource("/img/card_chip_gold.png")));
		mntmCarto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					JDCartao ifCartao = new JDCartao(frame);
					ifCartao.setVisible(true);
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		mntmCarto.setMnemonic('a');
		mnCadastro.add(mntmCarto);

		JMenuItem mntmConta = new JMenuItem("Conta");
		mntmConta.setIcon(new ImageIcon(JFPrincipal.class
				.getResource("/img/cash_stack.png")));
		mntmConta.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					JIFConta fdConta = new JIFConta(frame);
					fdConta.setVisible(true);
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
		mntmConta.setMnemonic('o');
		mnCadastro.add(mntmConta);

		JMenuItem mntmDiviso = new JMenuItem("Divis\u00E3o");
		mntmDiviso.setIcon(new ImageIcon(JFPrincipal.class
				.getResource("/img/timeline_marker.png")));
		mntmDiviso.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					JDDivisao fDivisao = new JDDivisao(frame);
					fDivisao.setVisible(true);
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		mntmDiviso.setMnemonic('D');
		mnCadastro.add(mntmDiviso);

		JMenuItem mntmLimite = new JMenuItem("Limite");
		mntmLimite.setIcon(new ImageIcon(JFPrincipal.class
				.getResource("/img/tubes.png")));
		mntmLimite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

			}
		});
		mntmLimite.setMnemonic('L');
		mnCadastro.add(mntmLimite);

		JMenuItem mntmUsuario = new JMenuItem("Usu\u00E1rio");
		mntmUsuario.setIcon(new ImageIcon(JFPrincipal.class
				.getResource("/img/user_green.png")));
		mntmUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					JDUsuario fUsuario = new JDUsuario(JFPrincipal.this);
					fUsuario.setVisible(true);
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		mntmUsuario.setMnemonic('U');
		mnCadastro.add(mntmUsuario);

		mnCadastro.addSeparator();

		JMenuItem mntmSair = new JMenuItem("Sair");
		mntmSair.setIcon(new ImageIcon(JFPrincipal.class
				.getResource("/img/door_in.png")));
		mntmSair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mntmSair.setMnemonic('S');
		mnCadastro.add(mntmSair);

		JMenu mnControle = new JMenu("Controle");
		mnControle.setMnemonic('o');
		menuBar.add(mnControle);

		mntmAcerto = new JMenuItem("Acerto");
		mntmAcerto.setMnemonic('A');
		mntmAcerto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JIFAcerto fAcerto = new JIFAcerto(JFPrincipal.this);
				fAcerto.setVisible(true);
			}
		});
		mnControle.add(mntmAcerto);

		JMenuItem mntmFatura = new JMenuItem("Fatura");
		mntmFatura.setMnemonic('F');
		mntmFatura.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					JIFFatura fFatura = new JIFFatura(JFPrincipal.this);
					fFatura.setVisible(true);
				} catch (ClassNotFoundException | SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		mnControle.add(mntmFatura);

		mntmComovime = new JMenuItem("Movimento");
		mntmComovime.setMnemonic('M');
		mntmComovime.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new JIFComovime(JFPrincipal.this).setVisible(true);
			}
		});
		mnControle.add(mntmComovime);

		JMenuItem mntmLancamento = new JMenuItem("Lan\u00E7amento");
		mntmLancamento.setMnemonic('L');
		mntmLancamento.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					new JIFLancamento(JFPrincipal.this).setVisible(true);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		mnControle.add(mntmLancamento);

		JMenu mnRelatrios = new JMenu("Relat\u00F3rios");
		mnRelatrios.setMnemonic('R');
		menuBar.add(mnRelatrios);

		mntmFatura_1 = new JMenuItem("Fatura");
		mntmFatura_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new JDRelFatura(frame, true).setVisible(true);
			}
		});
		mnRelatrios.add(mntmFatura_1);

	}

	public static Usuario getUsuario() {
		return usuario;
	}

	public static void setUsuario(Usuario usuario) {
		JFPrincipal.usuario = usuario;
	}

}
